import baseRequest from '../../request'
import {translateJSDOM} from '../../tool'

interface newsListConfig {
    news: any[],
    notice: any[]
}

export const getNewsList = () => {
    return new Promise(async (resolve) => {

        let newsList: newsListConfig = {
            news: [],
            notice: []
        }

        let res = await baseRequest.get('http://www.wuyiu.edu.cn/', '')

        // console.log(res.data);
        const $: any = translateJSDOM(res.data)

        // 获取到表格dom节点，并遍历每一个tr(每一行进行) => 获取内容
        $("#wp_news_w305  > table >tbody >tr").each((index: number, item: any) => {
            $(item.children).each((i: number, t: any) => {
                if (i === 1) {
                    // console.log($(t.children).attr('href'))
                    let res = {
                        id: index,
                        content: $(t.children).text(),
                        link: 'http://www.wuyiu.edu.cn/' + $(t.children).attr('href')
                    }
                    newsList.news.push(res)
                }
            })

        })

        $("#wp_news_w101  > table >tbody >tr").each((index: number, item: any) => {
            $(item.children).each((i: number, t: any) => {
                if (i === 1) {
                    // console.log($(t.children).attr('href'))
                    let res = {
                        id: index,
                        content: $(t.children).text(),
                        link: 'http://www.wuyiu.edu.cn/' + $(t.children).attr('href')
                    }
                    newsList.notice.push(res)
                }
            })

        })

        // console.log(newsList);
        resolve(newsList)
    })
}



